#include <my_header.h>
#include <string.h>
#define BUF_SIZE 64

int compare_files(const char* path1, const char* path2);

/* Usage:  */
int main(int argc, char *argv[]){                                  
    ARGS_CHECK(argc, 3);

    int ret = compare_files(argv[1], argv[2]);
    if(ret == 0) printf("Same \n");
    else         printf("Not Same \n");

    return ret;
}

int compare_files(const char* path1, const char* path2){
    int fd1 = open(path1, O_RDONLY);
    int fd2 = open(path2, O_RDONLY);

    ERROR_CHECK(fd1, -1, "file1 open");
    ERROR_CHECK(fd2, -1, "file2 open");

    char buf1[BUF_SIZE] = {0};
    char buf2[BUF_SIZE] = {0};

    int flag = 0;

    while(1){
        int read_num1 = read(fd1, buf1, sizeof(buf1));
        int read_num2 = read(fd2, buf2, sizeof(buf2));
        
        if(read_num1 < 0 || read_num2 < 0){
            flag = -1;
            break;
        }

        if(read_num1 == 0 && read_num2 == 0){
            break;
        }

        if(read_num1 != read_num2){
            flag = -1;
            break;
        }

        // if(strcmp(buf1, buf2) != 0) return -1;
        if(memcmp(buf1, buf2, read_num1) != 0){
            flag = -1;
            break;
        }
    }

    close(fd1);
    close(fd2);
    return flag;
}
